# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=libexodus
_realname2=seacas
_releases=2023-02-03
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=8.19
pkgrel=1
pkgdesc="EXODUS is a model developed to store and retrieve data for finite element analyses (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
conflicts=("${MINGW_PACKAGE_PREFIX}-seacas"
           "${MINGW_PACKAGE_PREFIX}-trilinos")
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-netcdf")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
url="https://sandialabs.github.io/seacas-docs"
license=('BSD')
source=("https://github.com/sandialabs/seacas/archive/v${_releases}.tar.gz"
        "cmake-fix.patch")
sha256sums=('8076d8efc064ef261bb64bd1d064088586d9197b94cbfb0bba199a78243ca9e8'
            '3f9f345dd0474e1e5831fc0ae69fe6fe3ab86eb2a90c52f96fd5c2babe05519f')
noextract=("v${_releases}.tar.gz")

prepare() {
  echo "Extracting v${_releases}.tar.gz ..."
  tar -xzf v${_releases}.tar.gz || true

  cd "${srcdir}"/${_realname2}-${_releases}
  sed -i 's:-pedantic::g' cmake-exodus

  patch -Np1 -i "${srcdir}/cmake-fix.patch"
}

build() {
  mkdir -p "${_realname2}-${_releases}/build-${MSYSTEM}" && cd "${_realname2}-${_releases}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      INSTALL_PATH=${MINGW_PREFIX} \
      THREADSAFE=ON \
      COMPILER=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "gnu" || echo "clang") \
      FC=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "gfortran" || echo "flang") \
      ../cmake-exodus \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DSeacas_ENABLE_TESTS=OFF \
      "${_extra_config[@]}"

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${_realname2}-${_releases}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}/seacas-${_releases}/packages/seacas/libraries/exodus"/COPYRIGHT "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  # Remove extra files
  rm -rf "${pkgdir}"${MINGW_PREFIX}/lib/external_packages
}
